package com.kaoshi.myweb.webstruts.support;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class MyInterceptor implements Interceptor {
	private String dbOrFile;
	
	@Override
	public void destroy() {
		System.out.println("MyInterceptor destroy");
	}

	@Override
	public void init() {
		System.out.println("MyInterceptor init");
		System.out.println(dbOrFile);
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		/*
		 * 监听Result执行前事件,
		 * 代码写在自定义interceptor的执行方法中的，放到actionInvocation.invoke()之前。
		 */
		PreResultListener preResultListener = new MyBeforeResult();
		ActionContext.getContext().getActionInvocation().addPreResultListener(preResultListener);
		
		System.out.println("action 执行前");
		if(dbOrFile.equals("file")){
			System.out.println("記錄到文本");
		}
		else {
			System.out.println("記錄到數據庫");
		}
		String result = invocation.invoke();
		System.out.println("action 执行后");
		return result;
	}

	public String getDbOrFile() {
		return dbOrFile;
	}

	public void setDbOrFile(String dbOrFile) {
		this.dbOrFile = dbOrFile;
	}

}
